using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameJam2012
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class SnakeGame : Microsoft.Xna.Framework.Game
    {
        public static int SCREEN_WIDTH = 1280;
        public static int SCREEN_HEIGHT = 768;
        public static int TILESIZE = 64;
        public static Random Random = new Random();

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D titleScreen;

        public static Texture2D PARTICLE;
        public static Texture2D WHITETILE;
        public static Texture2D BACKTILE;
        public static Texture2D WALLTILE;
        public static Texture2D SNAKE;
        public static Texture2D MEAT;
        public static Song SONG;
        public static SoundEffect INTRO;
        public static SoundEffect BLOOD;
        public static SoundEffect EATING;
        public static SoundEffect EATMEAT;
        public static SoundEffect FALLSOUND;
        public static SoundEffect JUMPING;
        public static SoundEffect LANDING;
        public static SoundEffect MENUCLICK;
        public static List<Texture2D> WALLTILES = new List<Texture2D>();
        public static List<Texture2D> SPIKETILES = new List<Texture2D>();
        public static List<Texture2D> SURFACETILES = new List<Texture2D>();
        public static Texture2D SAWBLADEBLOODY;
        public static Texture2D SAWBLADE;
        GameController gameController = new GameController();
        public static SpriteFont FONT;

        public static double HighScore = 0;

        bool titleScreenOn = true;

        public SnakeGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = SCREEN_WIDTH;
            graphics.PreferredBackBufferHeight = SCREEN_HEIGHT;
            //graphics.ToggleFullScreen();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            PARTICLE = Content.Load<Texture2D>("particle");
            WHITETILE = Content.Load<Texture2D>("white_square");
            BACKTILE = Content.Load<Texture2D>("CN_Background_Test");
            FONT = Content.Load<SpriteFont>("Font");
            WALLTILE = Content.Load<Texture2D>("wall");
            for(int i=1; i<=24; i++)
                WALLTILES.Add(Content.Load<Texture2D>("cobble_blood"+i));
            for (int i = 1; i <= 4; i++)
                SURFACETILES.Add(Content.Load<Texture2D>("Surface_cobble_blood" + i));
            for (int i = 1; i <= 10; i++)
                SPIKETILES.Add(Content.Load<Texture2D>("Spike_" + i));

            SAWBLADE = Content.Load<Texture2D>("saw_blade");
            SAWBLADEBLOODY = Content.Load<Texture2D>("bloody_saw_blade");

            MEAT = Content.Load<Texture2D>("meat");

            SNAKE = Content.Load<Texture2D>("CN_Main_Character");
            //INTRO = Content.Load<Song>("Sound/GuitarBegin");
            SONG = Content.Load<Song>("Sound/ConansDreamLoop");

            BLOOD = Content.Load<SoundEffect>("Sound/Blood");
            EATING = Content.Load<SoundEffect>("Sound/Eating");
            EATMEAT = Content.Load<SoundEffect>("Sound/EatMeat");
            FALLSOUND = Content.Load<SoundEffect>("Sound/FallSound");
            INTRO = Content.Load<SoundEffect>("Sound/GuitarBegin");
            JUMPING = Content.Load<SoundEffect>("Sound/Jumping");
            LANDING = Content.Load<SoundEffect>("Sound/Landing");
            MENUCLICK = Content.Load<SoundEffect>("Sound/MenuClick");

            titleScreen = Content.Load<Texture2D>("CN_Title_Screen");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            if (!titleScreenOn)
                gameController.Update(gameTime);
            else
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                {
                    titleScreenOn = false;
                    MENUCLICK.Play();
                    gameController.Initialize();
                }

            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.LinearWrap, DepthStencilState.None, RasterizerState.CullNone);
            if (!titleScreenOn) gameController.Draw(spriteBatch);
            else
            {
                spriteBatch.Draw(titleScreen, Vector2.Zero, null, Color.White);
                spriteBatch.DrawString(FONT, "Press ENTER to Begin", new Vector2(1280 / 2 - 128, 550), Color.White, 0f, Vector2.Zero, .5f, SpriteEffects.None, 1f);
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
